package cn.ixinjiu.libraryMangement.controller.systemAdmin.bookInfo;

import cn.ixinjiu.libraryMangement.entity.BookInfo;
import cn.ixinjiu.libraryMangement.service.IBookInfoService;
import cn.ixinjiu.libraryMangement.service.impl.BookInfoServiceImpl;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

/**
 * @Author: Dora.
 * @Date: 2023/05/24/11:22
 */
@WebServlet("/bookInfoAdd")
public class BookInfoAddServlet extends HttpServlet {
    private IBookInfoService bookInfoService = new BookInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-Type", "text/html;charset=utf-8");

        // 获取前端参数
        int id = Integer.valueOf((req.getParameter("id")));
        String bookName = req.getParameter("book_name");
        String bookPic = req.getParameter("book_pic");
        int sortId = Integer.valueOf((req.getParameter("sort_id")));
        String author = req.getParameter("author");
        String publisher = req.getParameter("publisher");
        Date publisherAt = Date.valueOf(req.getParameter("publisher_at"));
        String isbn = req.getParameter("isbn");
        Integer status = Integer.valueOf(req.getParameter("status"));
        BookInfo bookInfo = new BookInfo(id, bookName, bookPic, sortId, author, publisher, publisherAt, isbn, status);
        // 调用新增方法
        int add = bookInfoService.bookInfoAdd(bookInfo);
        // 返回数据
        JSONObject json = new JSONObject();
        if (add == 1) {
            json.put("code", "0");
            json.put("msg", "修改成功");
        } else {
            json.put("code", "1");
            json.put("msg", "修改失败");
        }
        // 输出到页面
        PrintWriter writer = resp.getWriter();
        writer.write((json.toString()));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
